﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.Diagnostics;
using System.Configuration;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability;

namespace EntLibContrib.Resource.Configuration.Manageability
{
	/// <summary>
	/// Represents a mapper for <see cref="ResourceSettings"/> configuration to Wmi.
	/// </summary>
	public static class ResourceSettingsWmiMapper
	{
		#region Public Static Methods
		/// <summary>
		/// Creates the <see cref="ResourceBlockSetting"/> instances that describe the configurationObject.
		/// </summary>
		/// <param name="configurationObject">The configuration object for instances that must be managed.</param>
		/// <param name="wmiSettings">A collection to where the generated WMI objects are to be added.</param>
		public static void GenerateWmiObjects(ResourceSettings configurationObject,
																					ICollection<ConfigurationSetting> wmiSettings)
		{
			wmiSettings.Add(new ResourceBlockSetting(configurationObject,
																							configurationObject.DefaultResourceManager));
		}

		/// <summary>
		/// Creates the <see cref="ResourceManagerSetting"/> instances that describe the configurationObject.
		/// </summary>
		/// <param name="data">The configuration object for instances that must be managed.</param>
		/// <param name="wmiSettings">A collection to where the generated WMI objects are to be added.</param>
		public static void GenerateResourceManagerWmiObjects(ResourceManagerDataBase data, ICollection<ConfigurationSetting> wmiSettings)
		{
			ResourceManagerData resourceManagerData = data as ResourceManagerData;
			if (resourceManagerData != null)
				wmiSettings.Add(new ResourceManagerSetting(data.Name, resourceManagerData.ResourceProvider));
			else
				wmiSettings.Add(new UnknownResourceManagerSetting(data.Name));
		}
		#endregion

		#region Internal Static Methods
		/// <summary>
		/// Registers the WMI types.
		/// </summary>
		internal static void RegisterWmiTypes()
		{
			try
			{
				ManagementEntityTypesRegistrar.SafelyRegisterTypes(typeof(ResourceBlockSetting),
					typeof(ResourceManagerSetting),
					typeof(UnknownResourceManagerSetting));
			}
			catch (ArgumentException ex)
			{
				// ignore error that states registry entry already exists
				if (Debugger.IsAttached)
					Debug.WriteLine(ex.Message);
			}
		}

		/// <summary>
		/// Save changes made to a <see cref="ResourceBlockSetting"/> instance
		/// </summary>
		/// <param name="resourceBlockSetting">The configuration object.</param>
		/// <param name="sourceElement">The parent configuration object.</param>
		/// <returns></returns>
		internal static bool SaveChanges(ResourceBlockSetting resourceBlockSetting, ConfigurationElement sourceElement)
		{
			ResourceSettings settings = (ResourceSettings)sourceElement;

			settings.DefaultResourceManager = resourceBlockSetting.DefaultResourceManager;

			return true;
		}
		#endregion
	}
}
